package pers.ree.demo.designpattern.memento;

/**
 * 看守者：
 * 负责在适当的时间将Originator对象的状态保存到memento对象或者从memento恢复，
 * 此外一般还用于保存memento对象，但是不会对memento进行操作或检查
 */
public class Caretaker {

    private Memento memento;


    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }


    public static void main(String[] args) {
        Originator originator = new Originator();
        originator.setCode(111);
        originator.setName("originator" + originator.getCode());

        System.out.println("before modify, originator: " + originator);

        Memento memento = originator.createMemeto();
        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(memento);

        originator.setCode(222);
        originator.setName("originator" + originator.getCode());

        System.out.println("after modify, originator: " + originator);

        originator.restoreFromMemeto(caretaker.getMemento());

        System.out.println("after restore, originator: " + originator);
    }
}
